package org.nokatag.tagjava;

import java.io.IOException;
import java.util.HashSet;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.system.Concest;
import org.nokatag.system.Function;
/**
 * 文件上传
 * @author rebin
 *
 */
public class FileUoload extends NokaTag{

	private static final long serialVersionUID = -1974065560012854302L;
	private String name=null;//名字
	private String id = null;//id
	private String filesize=null;//上传文件大小
	private String filetypedescription = null;//显示的可上传的文件类型
	private String filetypeextension = null;//实际可上传的文件类型
	private String uid = null;//用户自定义过滤id
	private String isdel = null;//是否可以删除
	private String type=null;//过滤类型，uid,sid,pid
	private String idreadio=null;//是否为单选
	private Integer showtype=null;//显示方式，1,为下载，2，为查看，0，为没有操作
	private String value=null;
	private String onchange=null;//在值改变时调用的方法
	private String uploadfunction = null;//上传外部调用方法
	private String readonly=null;//是否只读
	private String disabled = null;
	private String noShow="false";//不显不
	private Integer textsun=50;//备注字数
	private String title=null;//按钮显示的标题
	private static final Integer textsunMax=100;
	private String width = null;//宽度
	private String allownull="true";
	private String dataurl = null;
	
	private static final String NOKA_FILE_UOLOAD_JAVASCRIPT_FUNCTION = "NOKA_FILE_UOLOAD_JAVASCRIPT_FUNCTION";
	
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		String basePath = basepath(request);
		if(null!=dataurl){
			basePath = dataurl;
		}
		JspWriter jspOut = pageContext.getOut();
		StringBuilder bodystr = new StringBuilder();
		UUID uuid = UUID.randomUUID();
		if(this.getFiletypeextension()==null)
			  this.setFiletypeextension("*.jpg;*.gif;*.png");
		  //-------------------设置文件类型------------------
		  if(Concest.UPDOCTYPE==null){
			  Concest.UPDOCTYPE=new HashSet<String>();
		  }
		  for(String doctyp:getFiletypeextension().split(";")){
			  Concest.UPDOCTYPE.add(doctyp.substring(doctyp.lastIndexOf(".")+1,doctyp.length()));
		  }
		  String sessionid = uid+","+pageContext.getSession().getId()+","+uuid.toString().replace("-", "_");
		  bodystr.append(potjs(request));//加载Prototyp
		  bodystr.append(potuploadjs(request));//加载upfile.js
		  bodystr.append(selectCss(request));//加载Prototype
		  bodystr.append(nkBoxJsAndCss(request));
		  FormHtmlInsertid(getId(),getName(),getValue());
		  bodystr.append("<div id=\""+id+"_fileDiv\" style=\"float: left;\"></div><div id=\""+getId()+"_sDiv\" class=\"nk_input_sdDiv\" style=\"display:"+("true".equals(noShow)?"none":"inline-block")+"\"></div>");//加载Prototype
		  bodystr.append("<script type=\"text/javascript\">");
		  bodystr.append("Event.observe(document, 'dom:loaded', function() {");
		  bodystr.append("var upfs"+getId()+" = {");
		  bodystr.append("id:'"+getId()+"',");
		  bodystr.append("name:'"+getName()+"',");
		  bodystr.append("allownull:"+allownull+",");
		  bodystr.append("value:'"+(value==null?"":value)+"',");
		  bodystr.append("filesize:'"+(filesize==null?"512":filesize)+"',");
		  bodystr.append("filetypedescription:'"+(filetypedescription==null?"images(*.jpg,*.gif,*.png)":filetypedescription)+"',");
		  bodystr.append("filetypeextension:'"+(filetypeextension==null?"*.jpg;*.gif;*.png":filetypeextension)+"',");
		  bodystr.append("uid:'"+(uid==null?"99999999":uid)+"',");
		  bodystr.append("isdel:'"+("false".equalsIgnoreCase(isdel)?"no":"yes")+"',");
		  bodystr.append("type:'"+(type==null?"pid":type)+"',");
		  bodystr.append("idreadio:"+(idreadio==null?"false":idreadio)+",");
		  bodystr.append("showtype:'"+(showtype==null?"2":showtype)+"',");
		  bodystr.append("readonly:"+("readonly".equals(readonly))+",");
		  bodystr.append("disabled:"+("disabled".equals(disabled))+",");
		  bodystr.append("noShow:"+(noShow==null?"false":noShow)+",");
		  bodystr.append("textsun:'"+((textsun==null || textsun>textsunMax)?textsunMax:textsun)+"',");
		  bodystr.append("title:'"+(title==null?Function.lung(request,"nokatag_jsp_title"):title)+"',");
		  bodystr.append("rurl:'"+basePath+"',");
		  bodystr.append("width:"+LenNumber((null==width?"70":width))+",");
		  bodystr.append("sessionid:'"+sessionid+"',");
		  bodystr.append("onchange:function(fname,fid,frurl,fdurl){"+(onchange==null?"":""+onchange+"(fname,fid,frurl,fdurl);")+"}");
		  bodystr.append("};");
		  bodystr.append(" var upfsfieupload"+getId()+" = new nfileupload(upfs"+getId()+");");
		  bodystr.append(" upfsfieupload"+getId()+".show();");
		  bodystr.append("});");
//		  bodystr.append(" function noka_tag_upload_file_redata_"+getId()+"(fname,fid,frurl,fdurl){$('"+getId()+"').uploadRedata(fname,fid,frurl,fdurl);}");
//		  bodystr.append(" function noka_tag_upload_file_setvalue_"+getId()+"(fid){$('"+getId()+"').setValue(fid);}");
//		  bodystr.append(" function noka_tag_upload_file_pclose_"+getId()+"(){try{NokaBox.box.hide();}catch(e){}}");//关闭
//		  //--------------------------------------------------------------------------------------------------------------
		  if(request.getAttribute(NOKA_FILE_UOLOAD_JAVASCRIPT_FUNCTION)==null || request.getAttribute(NOKA_FILE_UOLOAD_JAVASCRIPT_FUNCTION).equals(false)){
				request.setAttribute(NOKA_FILE_UOLOAD_JAVASCRIPT_FUNCTION,true);
				bodystr.append(" function noka_tag_upload_file_redata(id,fname,fid,frurl,fdurl){$(id).uploadRedata(fname,fid,frurl,fdurl);}");
				bodystr.append(" function noka_tag_upload_file_setvalue(id,fid){$(id).setValue(fid);}");
				bodystr.append(" function noka_tag_upload_file_pclose(){try{NokaBox.box.hide();}catch(e){}}");//关闭
		  }
		  //---------------------------------------------------------------------------------------------------------------
		  if(uploadfunction!=null){
				bodystr.append(" function "+uploadfunction+"(){$('"+getId()+"').uploadFile()}");
			}
		  bodystr.append("</script>");
		try {
			jspOut.write(bodystr.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
		
	}

	public String getFilesize() {
		return filesize;
	}

	public void setFilesize(String filesize) {
		this.filesize = filesize;
	}

	public String getFiletypedescription() {
		return filetypedescription;
	}

	public void setFiletypedescription(String filetypedescription) {
		this.filetypedescription = filetypedescription;
	}

	public String getFiletypeextension() {
		return filetypeextension;
	}

	public void setFiletypeextension(String filetypeextension) {
		this.filetypeextension = filetypeextension;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIdreadio() {
		return idreadio;
	}

	public void setIdreadio(String idreadio) {
		this.idreadio = idreadio;
	}

	public String getIsdel() {
		return isdel;
	}

	public void setIsdel(String isdel) {
		this.isdel = isdel;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public Integer getShowtype() {
		return showtype;
	}

	public void setShowtype(Integer showtype) {
		this.showtype = showtype;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getUploadfunction() {
		return uploadfunction;
	}

	public void setUploadfunction(String uploadfunction) {
		this.uploadfunction = uploadfunction;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	
	public String getReadonly() {
		return readonly;
	}

	public void setReadonly(String readonly) {
		this.readonly = readonly;
	}

	public String getNoShow() {
		return noShow;
	}

	public void setNoShow(String noShow) {
		this.noShow = noShow;
	}

	public Integer getTextsun() {
		return textsun;
	}

	public void setTextsun(Integer textsun) {
		this.textsun = textsun;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAllownull() {
		return allownull;
	}

	public void setAllownull(String allownull) {
		this.allownull = allownull;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getDisabled() {
		return disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}

	public String getDataurl() {
		return dataurl;
	}

	public void setDataurl(String dataurl) {
		this.dataurl = dataurl;
	}

	
}
